import os

from PyQt5.QtWidgets import QWidget, QGridLayout, QLabel, QLineEdit, QPushButton, QFileDialog, QCheckBox

from batch_attestat_print import BatchAttestatPrint
from generate_attestat_functions import generate_attestat_face
from setup_attestat_face_sizes import SetupAttestatFaceSizes


class AttestatFace(QWidget):

    def send_data_to_generate_attestat(self):
        name = self.fio_input_field.text()
        school = self.school_input_field.text()
        year = self.year_input_field.text()
        director = self.director_input_field.text()
        date = self.date_input_field.text()
        attestat_number = self.attestat_number_input_field.text()
        is_necessary_to_print_back = self.print_background_checkbox.isChecked()
        generate_attestat_face(
            name=name,
            school=school,
            year=year,
            director=director,
            date=date,
            attestat_number=attestat_number,
            is_necessary_to_print_back=is_necessary_to_print_back
        )

    def update_status_bar(self, text):
        self.statusBar.showMessage(text)

    def batch_thread_face_print(self):
        filename, ok = QFileDialog.getOpenFileName(self, 'Выберите файл', os.getcwd(), "Excel File (*.xlsx *.ods)")
        if ok:
            self.thread = BatchAttestatFacePrint(filename, self.print_background_checkbox.checkState())
            self.thread.progress_signal.connect(self.update_status_bar)
            self.thread.start()

    def setup_attestat_face_sizes(self):
        self.setup_attestat_face_sizes_window = SetupAttestatFaceSizes()

    def __init__(self, statusBar):
        self.statusBar = statusBar
        super(QWidget, self).__init__()
        self.layout = QGridLayout(self)

        self.layout.addWidget(QLabel('ФИО ученика'), 0, 0)
        self.fio_input_field = QLineEdit()
        self.layout.addWidget(self.fio_input_field, 0, 1)

        self.layout.addWidget(QLabel('Название школы в винительном падеже'), 1, 0)
        self.school_input_field = QLineEdit()
        self.layout.addWidget(self.school_input_field, 1, 1)

        self.layout.addWidget(QLabel('Год окончания школы'), 2, 0)
        self.year_input_field = QLineEdit()
        self.layout.addWidget(self.year_input_field, 2, 1)

        self.layout.addWidget(QLabel('ФИО руководителя организации'), 3, 0)
        self.director_input_field = QLineEdit()
        self.layout.addWidget(self.director_input_field, 3, 1)

        self.layout.addWidget(QLabel('Дата выдачи аттестата'), 4, 0)
        self.date_input_field = QLineEdit()
        self.layout.addWidget(self.date_input_field, 4, 1)

        self.layout.addWidget(QLabel('Номер аттестата'), 5, 0)
        self.attestat_number_input_field = QLineEdit()
        self.layout.addWidget(self.attestat_number_input_field, 5, 1)

        self.print_attestat_button = QPushButton('Напечатать аттестат')
        self.print_attestat_button.released.connect(self.send_data_to_generate_attestat)
        self.layout.addWidget(self.print_attestat_button, 6, 0, 1, 2)

        self.batch_print_attestat_button = QPushButton('Пакетная печать...')
        self.batch_print_attestat_button.released.connect(self.batch_thread_face_print)
        self.layout.addWidget(self.batch_print_attestat_button, 7, 0, 1, 2)

        self.settings_attestat_button = QPushButton('Настроить габариты')
        self.settings_attestat_button.released.connect(self.setup_attestat_face_sizes)
        self.layout.addWidget(self.settings_attestat_button, 8, 0, 1, 2)

        self.print_background_checkbox = QCheckBox('Печатать фон')
        self.print_background_checkbox.setChecked(True)
        self.layout.addWidget(self.print_background_checkbox, 9, 0, 1, 2)


class BatchAttestatFacePrint(BatchAttestatPrint):

    def __init__(self, filename, is_necessary_to_print_back=True, parent=None):
        super().__init__(filename, is_necessary_to_print_back, parent)
        self.start_message, self.print_error_message, self.progress_message = \
            'Начало печати лицевых сторон аттестатов...', \
                'В строке недостаточно данных, аттестат не будет напечатан', \
                'Печать аттестата'
        self.generate_function = generate_attestat_face
        self.folder_to_store_images = 'faces'
        self.pdf_filename = 'faces.pdf'

    def check_row(self, row):
        if '' in row[:6]:
            return False
        return True

    def run(self):
        super().run()
